package com.pri.chat.base;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.netease.nim.uikit.net.SharedHelper;
import com.pri.chat.MainActivity;
import com.pri.chat.MainDaoHangActivity;
import com.pri.chat.R;
import com.pri.chat.activity.LoginActivity;
import com.pri.chat.application.APP;
import com.pri.chat.bean.KeyAndValueEntity;
import com.pri.chat.database.greendao.KeyAndValueEntityDao;
import com.pri.chat.utils.statusbar.StatusBarUtil;
import com.vondear.rxtool.RxActivityTool;

import java.util.List;

import androidx.appcompat.app.AppCompatActivity;


public class GuangGaoActivity extends AppCompatActivity {
    private SharedHelper sharedHelper;
    private Context mContext;
    private ImageView ggImage;
    private TextView dumiao;

    private boolean isFinish = false;
    private TimeCount time;
    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guanggao);
        mContext = this;
        StatusBarUtil.setStatusBar(this, true, R.color.transparent, false, false);


        ggImage = findViewById(R.id.ggImage);
        dumiao = findViewById(R.id.dumiao);
        path = getIntent().getExtras().getString("path");
        time = new TimeCount(4000, 1000);
//        Glide.with(mContext).load(path).into(ggImage);
        dumiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<KeyAndValueEntity> list = APP.getKeyAndValueEntityDao().queryBuilder().where(KeyAndValueEntityDao.Properties.TypeKey.eq("daohang")).build().list();
                if(list == null || list.size() == 0){
                    RxActivityTool.skipActivity(GuangGaoActivity.this, MainDaoHangActivity.class);
                }else{
                    if("hasDone".equals(list.get(0).getValue())){
                        RxActivityTool.skipActivity(GuangGaoActivity.this, MainActivity.class);
                    }else{
                        RxActivityTool.skipActivity(GuangGaoActivity.this, MainDaoHangActivity.class);
                    }
                }
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        time.start();
    }

    class TimeCount extends CountDownTimer {

        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            dumiao.setText("跳过 "+(millisUntilFinished / 1000)+"S");
        }

        @Override
        public void onFinish() {
            if (!isFinish) {
                SharedPreferences sp = getSharedPreferences("login", Context.MODE_PRIVATE);
                boolean isFirst = sp.getBoolean("isFirst", true);
                if (isFirst) {
                    Intent intent = new Intent(GuangGaoActivity.this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                    finish();
                } else {
                    List<KeyAndValueEntity> list = APP.getKeyAndValueEntityDao().queryBuilder().where(KeyAndValueEntityDao.Properties.TypeKey.eq("daohang")).build().list();
                    if(list == null || list.size() == 0){
                        RxActivityTool.skipActivity(GuangGaoActivity.this, MainDaoHangActivity.class);
                    }else{
                        if("hasDone".equals(list.get(0).getValue())){
                            RxActivityTool.skipActivity(GuangGaoActivity.this, MainActivity.class);
                        }else{
                            RxActivityTool.skipActivity(GuangGaoActivity.this, MainDaoHangActivity.class);
                        }
                    }
                    finish();
                }
                dumiao.setText("跳过 0S");
            }


        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isFinish = true;
        time.cancel();
    }
}
